package com.pk.platform.business.privilege.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.pk.platform.business.core.dao.ITreeMakerDao;
import com.pk.platform.business.privilege.dao.IMenuDao;
import com.pk.platform.business.privilege.service.IMenuService;
import com.pk.platform.business.privilege.service.IPrivilegeService;
import com.pk.platform.domain.privilege.Menu;
import com.pk.platform.domain.privilege.Privilege;
import com.pk.platform.domain.privilege.User;
import com.pk.platform.util.XmlUtil;
import com.pk.platform.util.constant.Constant;
import com.pk.platform.util.filter.SystemContext;

public class MenuServiceImpl implements IMenuService {

	private IMenuDao menuDao;
	
	private ITreeMakerDao treeMakerDao;
	
	private IPrivilegeService privilegeService;
	
	public List<Menu> queryTopMenu(Long id) {
		List<Menu> list = menuDao.queryTopMenu(id);
		List<Menu> result = new ArrayList<Menu>();
		User u = SystemContext.getUser();
		Map<String, String> privilege = u.getPrivilege();
		for(Menu m : list)
		{
			if(privilege.get(m.getId().toString()) != null)
			{
				result.add(m);
			}
		}
		return result;
	}
	
	public String getMenuTreeById(String menuId)
	{
		String[] field = new String[]{"menuName", "id", "parentId", "url", null, "sort"};
		String condition = " and isShow = '" + Constant.Y + "'";
		String tree = treeMakerDao.getTree("p_menu", menuId, field, true, false, null, null, condition);
		tree = XmlUtil.privilegeFilter(tree);
		return tree;
	}
	
	public Menu queryMenuById(Long id)
	{
		return menuDao.get(Menu.class, id);
	}
	
	public void delMenu(Long id)
	{
		menuDao.delete(Menu.class, id);
		privilegeService.delPrivilege(id.toString());
	}
	
	public void addMenu(Menu menu)
	{
		menuDao.add(menu);
		
		Privilege privilege = new Privilege();
		privilege.setId(menu.getId().toString());
		privilege.setPrivilegeName(menu.getMenuName());
		privilege.setParentId(menu.getParentId().toString());
		privilege.setUrl(menu.getUrl());
		privilege.setSort(menu.getSort());
		privilege.setIsShow(menu.getIsShow());
		privilege.setType(Constant.MENU);
		menuDao.add(privilege);
	}
	
	public void updateMenu(Menu menu)
	{
		//修改菜单
		Menu m = menuDao.get(Menu.class, menu.getId());
		m.setMenuName(menu.getMenuName());
		m.setParentId(menu.getParentId());
		m.setUrl(menu.getUrl());
		m.setIsShow(menu.getIsShow());
		m.setSort(menu.getSort());
		
		//修改权限
		Privilege privilege = menuDao.get(Privilege.class, menu.getId().toString());
		privilege.setPrivilegeName(menu.getMenuName());
		privilege.setParentId(menu.getParentId().toString());
		privilege.setUrl(menu.getUrl());
		privilege.setIsShow(menu.getIsShow());
		privilege.setSort(menu.getSort());
	}
	
	public String getAllMenuTree()
	{
		String[] field = new String[]{"menuName", "id", "parentId", "url", null, "sort"};
		String tree = treeMakerDao.getTree("p_menu", "1", field, true, false, null, null);
		return tree;
	}
	
	public boolean hasChildren(Long menuId)
	{
		int count = menuDao.queryChildrenCountByMenuId(menuId);
		if(count > 0)
			return true;
		return false;
	}
	
	public List<Menu> queryMenuListExcludeItself(Long menuId)
	{
		return menuDao.queryMenuListExcludeItself(menuId);
	}
	
	public List<Menu> querySysMenu()
	{
		return menuDao.querySysMenu();
	}
	
	
	

	public IMenuDao getMenuDao() {
		return menuDao;
	}

	public void setMenuDao(IMenuDao menuDao) {
		this.menuDao = menuDao;
	}

	public ITreeMakerDao getTreeMakerDao() {
		return treeMakerDao;
	}

	public void setTreeMakerDao(ITreeMakerDao treeMakerDao) {
		this.treeMakerDao = treeMakerDao;
	}

	public IPrivilegeService getPrivilegeService() {
		return privilegeService;
	}

	public void setPrivilegeService(IPrivilegeService privilegeService) {
		this.privilegeService = privilegeService;
	}

}
